<?php

/*
 * @author Ebrain Cedeno <ebraincedeno@gmail.com>
 */

class TEdit extends TWidget{
    
    public $Text;
    public $Type="text";
    public $ReadOnly = false;
    public $Disabled = false;
    public $IsHidden = false;
    
    public $KeyUp = '';
    public $KeyPress = '';
    public $KeyDown = '';
    
    public function __construct() {
        parent::__construct(__CLASS__);
        $this->Width = 200;
        $this->Height = 25;
    }

    protected  function getReadOnly(){
        if ($this->ReadOnly == true){
            return 'readOnly="true"'; 
        }
        else{
            return '';
        }
        
    }

    protected function getDisabled(){
        if ($this->Disabled == true){
            return 'disabled';
        }
        else{
            return '';
        }
        
    }
    
    protected function keyUp(){
        if ($this->KeyUp != ''){
            return '
                <script type="text/javascript">
                    $(document).ready(function(event){
                        $("#'.$this->Id.'").keyup(function(event){
                            '.$this->KeyUp.'
                        });
                    });
                </script>';
        }
    }
    
    protected function KeyPress(){
        
        if ($this->KeyPress != ''){
            return '
                <script type="text/javascript">
                    $(document).ready(function(event){
                        $("#'.$this->Id.'").keypress(function(event){
                            '.$this->KeyPress.'
                        });
                    });
                </script>
                ';
        }
        
    }
    protected function KeyDown(){
        
        if ($this->KeyDown != ''){
            return '
                <script type="text/javascript">
                    $(document).ready(function(event){
                        $("#'.$this->Id.'").keydown(function(event){
                            '.$this->KeyDown.'
                        })
                    });
                </script>
                ';
        }
        
    }

    public function show(){
        
        if ($this->IsHidden){
            return '<input name="'.$this->Name.'" id="'.$this->Id.'" type="hidden" value="'.$this->Text.'" />';
        }
        else{
        
            if (!array_key_exists('style', $this->OptionsAditional)){
                $this->OptionsAditional['style'] = array('width:'.$this->Width.'px','height:'.$this->Height.'px');
            }
            else{
                $this->OptionsAditional['style'][] = 'width:'.$this->Width.'px';
                $this->OptionsAditional['style'][] = 'height:'.$this->Height.'px';
            }
        
            if ($this->Visible == true){
                $html = '<input name="'.$this->Name.'" id="'.$this->Id.'" type="'.$this->Type.'" value="'.$this->Text.'" '.$this->getOptionsAditional().' '.$this->getReadOnly().' '.$this->getDisabled().' />'."\n";
                $html .= $this->keyUp()."\n";
                $html .= $this->KeyPress()."\n";
                $html .= $this->KeyDown();
                return $html;
            }
        } 
    }
}
?>